package base;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

public class CollectionDemo {
    public static void main(String[] args) {
        List<String> collection = new ArrayList<>();

        collection.add("aaaa");
        collection.add("bbbb");

        Iterator<String> iterator = collection.iterator();
        while (iterator.hasNext()) {
            String s = iterator.next();
          /*  if (s.equals("aaaa")) {
                collection.add("cccc");  //java.util.ConcurrentModificationException
            }*/
            System.out.println(s);
        }


        //使用for循环不会发生并发修改异常
        for (int i = 0; i < collection.size(); ++i) {
            String s = collection.get(i);
            if (s.equals("aaaa")) {
                collection.add("cccc");
            }
            System.out.println(s);
        }
    }
}
